package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bean.BookBack;
import com.example.service.BookBackService;
import com.example.until.Result;
import com.example.until.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @title: BookBackController
 * @Author wjf
 * @Date: 2022/11/19 15:51
 * @Version 1.0
 */
@CrossOrigin
@RestController
@RequestMapping("/back")
public class BookBackController {

    @Autowired
   private BookBackService bookBackService;

    @PostMapping("/save")
    public Result add(@RequestBody BookBack bookBack){
        String userName = TokenUtils.getCurrentUser().getNickname();
        bookBack.setUserName(userName);
        boolean save = bookBackService.save(bookBack);
        if (save){
            return Result.ok();
        }
        return Result.fail();
    }

    @GetMapping("/page")
    public Result page(
            @RequestParam Integer current,
            @RequestParam Integer pageSize,
            @RequestParam String  bookName){
        IPage<BookBack> page = new Page<>(current,pageSize);
        IPage<BookBack> page1 = bookBackService.findPage(page,bookName);
        return Result.ok(page1);
    }

    @PutMapping("/update")
    public Result update(@RequestBody BookBack bookBack){
        bookBack.setStatus(1);
        boolean b = bookBackService.updateById(bookBack);
        if (b){
            return Result.ok();
        }
       return Result.fail();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        boolean b = bookBackService.removeById(id);
        if (b){
            return Result.ok();
        }
          return Result.fail();
    }

    @DeleteMapping("/delete/batch/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        boolean b = bookBackService.removeByIds(ids);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }
}
